/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.generacion;

import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.apache.ddlutils.model.Database;
import pfc.comun.Proyecto;
import pfc.modelocg.CGForm;
import pfc.modelocg.CGModel;
import pfc.modeloer.ERModel;

/**
 *
 * @author xandru
 */
class GeneradorUpdateServlet extends GeneradorServlet{

    public GeneradorUpdateServlet(Proyecto pro, Database db, ERModel er, CGModel cg) {
        super(pro,db,er,cg);
    }

    void addRequest(ArrayList<String> methods,CGForm form) {
        String claseDTO = StringUtils.capitalize(form.getEntityName());
        String claseDAO = claseDTO + "DAO";

        methods.add("protected void processRequest(HttpServletRequest request, HttpServletResponse response)");
        methods.add("throws ServletException, IOException {");
        methods.add("PrintWriter out = response.getWriter();");
        methods.add( claseDTO + " obj = new " + claseDTO + "();");
        methods.add("out.println(\"Muestra del servlet de actualizacion \" + this.getClass().getCanonicalName());");
         methods.add("try {");
        methods.add("for (Enumeration e = request.getParameterNames();e.hasMoreElements();) {");
        methods.add("String key=(String) e.nextElement();");
        methods.add("String value = request.getParameter(key);");

//methods.add("System.out.println(\"antes de putPropertyValue \" + key + \" \" + value + \"<br>\");");
        methods.add("obj.putPropertyValue(key,value);");
//methods.add("System.out.println(\"despues de putPropertyValue \" + key + \" \" + value + \"<br>\");");

        methods.add("}");
        methods.add("}");
        methods.add("catch (Exception e) {");
        methods.add("out.println(\"Excepcion \" +  e.getClass().getCanonicalName() + \": \" + e.getMessage());");
        methods.add("}");
        methods.add("try {");
        methods.add("for (PropertyDescriptor pd:PropertyUtils.getPropertyDescriptors(obj)) {");
        methods.add(" String key  = pd.getName();");
        methods.add(" String value = obj.stringPropertyValue(key);");
        methods.add("  out.println(key + \" = \" + value);");
        methods.add("}");
        methods.add("}");
        methods.add("catch (Exception e) {");
        methods.add("out.println(e.getMessage());");
        methods.add("}");

        methods.add("DataSource ds = DataSourceFactory.getDataSourceFromJNDI(\"jdbc/" + this._proyecto.getNombre() + "\");");
        methods.add(claseDAO + " objDAO = new " + claseDAO + "(ds);");
        methods.add("out.println(\"Se ha usado el objeto DAO \" + objDAO);");
        methods.add("try {");        
        methods.add("objDAO.update(obj.getIdentity(),obj);");
        methods.add("} catch (Exception e) {");
        methods.add("out.println(\"Excepcion \" +  e.getClass().getCanonicalName() + \": \" + e.getMessage());");
        methods.add("}");
        methods.add("}");
    }

}
